import store from '../store';

/**
 * 去除驼峰，转为小写
 * @param {string} str 要转换的驼峰字符串
 * @param {string} sep 连接符,例如'-'或者'_'
 * @returns {string} 返回小写字符串
 */
export function decamelize(str, sep = '-') {
  return str
    .replace(/([a-z\d])([A-Z])/g, '$1' + sep + '$2')
    .replace(/([A-Z]+)([A-Z][a-z\d]+)/g, '$1' + sep + '$2')
    .toLowerCase();
}

// 传入px渲染缩放后的px
export const realPx = (num) => {
  let clientW = window.innerWidth;
  let per = clientW / 1920;
  return parseInt(num * per);
};

// 手动开启loading
export const showLoading = () => {
  store.commit('common/update', ['loadingCount', store.state.common.loadingCount + 1]);
};
// 手动关闭loading
export const hideLoading = () => {
  if (store.state.loadingCount > 1) {
    store.commit('common/update', ['loadingCount', store.state.common.loadingCount - 1]);
  } else {
    store.commit('common/update', ['loadingCount', 0]);
  }
};
// 手动关闭所有loading
export const hideLoadingAll = () => {
  store.commit('common/update', ['loadingCount', 0]);
};
